package cn.kgc.controller;

import cn.kgc.base.entity.CartSkuInfo;
import cn.kgc.entity.*;
import cn.kgc.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.kgc.dto.PmsSkuInfoAddDTO;
import cn.kgc.dto.PmsSkuInfoUpdateDTO;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.entity.PmsSkuInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * sku信息(PmsSkuInfo)表控制层
 *
 * @author makejava
 * @since 2022-12-05 09:38:31
 */
@RestController
@RequestMapping("/pms/SkuInfo")
@Api
@Validated
public class PmsSkuInfoController {
    /**
     * 服务对象
     */
    @Resource
    private PmsSkuInfoService pmsSkuInfoService;
    @Resource
    private PmsCategoryService pmsCategoryService;
    @Resource
    private PmsBrandService pmsBrandService;
    @Resource
    private PmsSpuInfoService pmsSpuInfoService;
    @Resource
    private PmsSkuSaleAttrValueService pmsSkuSaleAttrValueService;

    /**
     * 给购物车模块提供的商品信息接口
     */
    @GetMapping("/getCart")
    @ApiOperation(value = "给购物车模块提供的商品信息接口", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "skuId", value = "skuId",  dataType = "Long", paramType = "query")
    public CartSkuInfo getCartSkuInfo(@RequestParam(name = "skuId") Long skuId) {
        PmsSkuInfo pmsSkuInfo = pmsSkuInfoService.getById(skuId);
        long spuId = pmsSkuInfo.getSpuId();
        String defaultImage = pmsSkuInfo.getSkuDefaultImg();
        BigDecimal price = pmsSkuInfo.getPrice();
        String title = pmsSkuInfo.getSkuTitle();
        //根据skuid查询销售属性
        LambdaQueryWrapper<PmsSkuSaleAttrValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsSkuSaleAttrValue::getSkuId, skuId);
        List<PmsSkuSaleAttrValue> pmsSkuSaleAttrValues = pmsSkuSaleAttrValueService.list(wrapper);
        //将查询查来的销售属性转换为字符串属性名
        List<String > attrs=pmsSkuSaleAttrValues.stream().map(pmsSkuSaleAttrValue -> {
           String attr=pmsSkuSaleAttrValue.getAttrName()+":"+pmsSkuSaleAttrValue.getAttrValue();
           return attr;
       }).collect(Collectors.toList());

        CartSkuInfo cartSkuInfo = CartSkuInfo.builder()
                .skuId(skuId)
                .spuId(spuId)
                .image(defaultImage)
                .price(price)
                .skuAttr(attrs)
                .title(title)
                .build();

        return cartSkuInfo;
    }

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数，默认5条", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "skuName", value = "sku名", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "catalogId", value = "商品分类id,默认值0", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "brandId", value = "品牌id,默认值0", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "spuId", value = "spuId,默认值0", paramType = "query", dataType = "int"),
    })
    @CrossOrigin
    public IPage<PmsSkuInfo> selectAll(@RequestParam(defaultValue = "1") int current,
                                       @RequestParam(defaultValue = "5") int pageSize,
                                       @RequestParam(defaultValue = "") String skuName,
                                       @RequestParam(defaultValue = "0") int catalogId,
                                       @RequestParam(defaultValue = "0") int brandId,
                                       @RequestParam(defaultValue = "0") int spuId) {

        LambdaQueryWrapper<PmsSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(skuName),PmsSkuInfo::getSkuName, skuName);
        queryWrapper.eq(catalogId != 0,PmsSkuInfo::getCatalogId, catalogId);
        queryWrapper.eq(brandId != 0,PmsSkuInfo::getBrandId, brandId);
        queryWrapper.eq(spuId != 0,PmsSkuInfo::getSpuId, spuId);
        IPage<PmsSkuInfo> page = new Page(current, pageSize);
        pmsSkuInfoService.page(page, queryWrapper);
        List<PmsSkuInfo> pmsSkuInfos=page.getRecords();
        for (PmsSkuInfo pmsSkuInfo : pmsSkuInfos) {
            long brandIdTemp=pmsSkuInfo.getBrandId();
            long catalogIdTemp=pmsSkuInfo.getCatalogId();
            long spuIdTemp=pmsSkuInfo.getSpuId();
            PmsCategory pmsCategory=pmsCategoryService.getById(catalogIdTemp);
            if (pmsCategory!=null){
                pmsSkuInfo.setCatalogName(pmsCategory.getName());
            }
            PmsBrand pmsBrand=pmsBrandService.getById(brandIdTemp);
            if (pmsBrand!=null){
                pmsSkuInfo.setBrandName(pmsBrand.getName());
            }
            PmsSpuInfo pmsSpuInfo=pmsSpuInfoService.getById(spuIdTemp);
            if (pmsSpuInfo!=null){
                pmsSkuInfo.setSpuName(pmsSpuInfo.getSpuName());
            }
        }
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public PmsSkuInfo selectOne(@PathVariable Serializable id) {
        return this.pmsSkuInfoService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param pmsSkuInfoDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody PmsSkuInfoAddDTO pmsSkuInfoDTO) {
        PmsSkuInfo pmsSkuInfo = new PmsSkuInfo();
        BeanUtils.copyProperties(pmsSkuInfoDTO, pmsSkuInfo);
        return this.pmsSkuInfoService.save(pmsSkuInfo);
    }

    /**
     * 修改数据
     *
     * @param pmsSkuInfoDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody PmsSkuInfoUpdateDTO pmsSkuInfoDTO) {
        PmsSkuInfo pmsSkuInfo = this.pmsSkuInfoService.getById(pmsSkuInfoDTO.getId());
        BeanUtils.copyProperties(pmsSkuInfoDTO, pmsSkuInfo);
        return this.pmsSkuInfoService.updateById(pmsSkuInfo);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.pmsSkuInfoService.removeById(id);
    }
}
